#pragma once

#include <iostream>
#include <sys/epoll.h>
#include <unistd.h>

// 封装epoll类
class Epoll
{
public:
    static const int default_size = 256;
public:
    static int EpollCreate()
    {
        int epfd = epoll_create(default_size);
        if(epfd > 0) return epfd; 
        exit(5); // 如果epfd为负，则创建失败，直接exit
    }

    static bool EpollCtl(int epfd, int op, int sock, uint32_t events)
    {
        // 初始化epoll_event
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = sock;

        int ret = epoll_ctl(epfd, op, sock, &ev);
        return ret == 0; // 等于0则成功
    }

    static int EpollWait(int epfd, struct epoll_event evs_array[], int num, int timeout)
    {
        // 对于返回值：有几个fd就绪，就返回几
        // 如果就绪的fd非常多，数组没法拿完？—— 一次拿不玩，下回拿
        return epoll_wait(epfd, evs_array, num, timeout);
    }
};